import GoogleMobileAds
import SwiftUI

struct NativeContentView: View {
    @StateObject private var nativeViewModel = NativeAdViewModel()
    let navigationTitle: String

    var body: some View {
        ScrollView {
            VStack(spacing: 20) {
                if nativeViewModel.isAdLoaded {
                    NativeAdView(nativeViewModel: nativeViewModel)
                        .frame(height: 150)
                    
                  
                } else {
                
                   // Text("load error")
                }
            }
            .padding(.horizontal, 20)
            .onAppear {
                refreshAd()
            }
            .navigationTitle(navigationTitle)
        }
    }

    private func refreshAd() {
        nativeViewModel.refreshAd()
    }
}

struct NativeContentView_Previews: PreviewProvider {
    static var previews: some View {
        NativeContentView(navigationTitle: "Native")
    }
}

private struct NativeAdView: UIViewRepresentable {
    typealias UIViewType = GADNativeAdView

    @ObservedObject var nativeViewModel: NativeAdViewModel

    func makeUIView(context: Context) -> GADNativeAdView {
        return Bundle.main.loadNibNamed("NativeAdView", owner: nil, options: nil)?.first as! GADNativeAdView
    }

    func updateUIView(_ nativeAdView: GADNativeAdView, context: Context) {
        guard let nativeAd = nativeViewModel.nativeAd else { return }

        adLog("this is an ad title: \(nativeAd)")
        (nativeAdView.headlineView as? UILabel)?.text = nativeAd.headline
        nativeAdView.mediaView?.mediaContent = nativeAd.mediaContent
        (nativeAdView.bodyView as? UILabel)?.text = nativeAd.body
        (nativeAdView.iconView as? UIImageView)?.image = nativeAd.icon?.image
        (nativeAdView.starRatingView as? UIImageView)?.image = imageOfStars(from: nativeAd.starRating)
        (nativeAdView.storeView as? UILabel)?.text = nativeAd.store
        (nativeAdView.priceView as? UILabel)?.text = nativeAd.price
        (nativeAdView.advertiserView as? UILabel)?.text = nativeAd.advertiser
        (nativeAdView.callToActionView as? UIButton)?.setTitle(nativeAd.callToAction, for: .normal)
        nativeAdView.callToActionView?.isUserInteractionEnabled = false
        nativeAdView.nativeAd = nativeAd
        

        
    }


    private func imageOfStars(from starRating: NSDecimalNumber?) -> UIImage? {
        guard let rating = starRating?.doubleValue else {
            return nil
        }
        if rating >= 5 {
            return UIImage(named: "stars_5")
        } else if rating >= 4.5 {
            return UIImage(named: "stars_4_5")
        } else if rating >= 4 {
            return UIImage(named: "stars_4")
        } else if rating >= 3.5 {
            return UIImage(named: "stars_3_5")
        } else {
            return nil
        }
    }
}
